import BaseAnimationPainting from "./BaseAnimationPainting";

export default class BaseLifeCyclePainting extends BaseAnimationPainting {

    constructor() {
        super();
        this.onInit();
        let status = BaseLifeCyclePainting.INIT;
        this.controller = {
            setStatus: s => {
                switch (s) {
                    case BaseLifeCyclePainting.INIT:
                        this.onInit();
                        break;
                    case BaseLifeCyclePainting.RUNNING:
                        this.onRunningStart();
                        break;
                    case BaseLifeCyclePainting.DEATH:
                        this.onDeath();
                        break;
                    case BaseLifeCyclePainting.DESTROYED:
                        this.onDestroyed();
                        break;
                    default:
                        return;
                }
                status = s;
                s === BaseLifeCyclePainting.DESTROYED && this.afterDestroyed();
            }
        };
        this.getStatus = () => status;
        this.initEffects = [];
        this.deathEffects = [];
        this.runningEffects = [];
    }

    nextFrame(painter) {
        super.nextFrame(painter);
        switch (this.getStatus()) {
            case BaseLifeCyclePainting.INIT:
                this.nextFrameOnInit(this.controller);
                this.effects = this.initEffects;
                break;
            case BaseLifeCyclePainting.RUNNING:
                this.nextFrameOnRunning(this.controller);
                this.effects = this.runningEffects;
                break;
            case BaseLifeCyclePainting.DEATH:
                this.nextFrameOnDeath(this.controller);
                this.effects = this.deathEffects;
                break;
            default:
            case BaseLifeCyclePainting.DESTROYED:
        }
    }

    /**
     * @param lifeCycle {number}
     * @param effect
     */
    addLifeCycleEffect(lifeCycle, effect) {
        switch (lifeCycle) {
            case BaseLifeCyclePainting.INIT:
                this.initEffects.push(effect);
                break;
            case BaseLifeCyclePainting.RUNNING:
                this.runningEffects.push(effect);
                break;
            case BaseLifeCyclePainting.DEATH:
                this.deathEffects.push(effect);
                break;
            default:
            case BaseLifeCyclePainting.DESTROYED:
        }
        return this;
    }

    nextFrameOnInit(controller) {
    }

    nextFrameOnRunning(controller) {
    }

    nextFrameOnDeath(controller) {
    }

    onInit() {
    }

    onRunningStart() {
    }

    onDeath() {
    }

    onDestroyed() {
    }

    afterDestroyed() {
    }
}

BaseLifeCyclePainting.INIT = 0;
BaseAnimationPainting.RUNNING = 1;
BaseAnimationPainting.DEATH = 2;
BaseAnimationPainting.DESTROYED = 3;